π Activity 1 β Search students by house
π Understand the data
You are given a 2D list called students.
Each record contains:
- First name [item 0]
- House name [item 1]
- House points [item 2]
students = [
["Cassie", "Robertson", 33],
["Emily", "Wilkinson", 31],
["Lydia", "Beale", 43],
["Nerys", "Jackson", 22],
["Parul", "Cross", 38],
["Amelia", "Robertson", 28],
["Sophie", "Wilkinson", 34],
["Hannah", "Beale", 39],
["Freya", "Jackson", 26],
["Aisha", "Cross", 35]
]
π What should happen
- The user will be asked to input a house at TGGS
- The program will then search all records within the list for a match
- If a match is found, the student's name is output
Enter a house at TGGS: Robertson Students in Robertson are: Cassie Amelia
π (1) Create the user input
Beneath the 2D list you must write a line of code where:- A variable is created called house
- An input is assigned to this variable
- The input prompts the user to "Input the name of a house at TGGS: "
Click to see how the code should look
house = input("Enter a house at TGGS: ")
π (2) Output a message before displaying students in that house
Beneath the code where the user inputs a house, there should be an output.
This will confirm the house that has been selected.
e.g
Students in Robertson:
or
Students in Cross:
Click to see how the code should look
print(f"Students in {house}")
π (3) Write the conditions for a for loop
In order to search for students in the selected house:
- The code must contain a for loop
- The for loop will iterate as many times as there are student records in the list
Click to see how the code should look
for i in range(0, 10):
π (4) Compare the house in each record with the user input
In each iteration of the for loop, the program should check the record that currently matches i
It will then look at the house listed in this record, to see if it is a match for the house the user entered
Click to see how the code should look
for i in range(0, 10):
if students[i][1] == house:
π (5) Output the name of each student in the house
If the record contains the name of the house the user has searched for...
The name of the student will be output.
Click to see how the code should look
for i in range(0, 10):
if students[i][1] == house:
print(students[i][0])
π Activity 2 β Find a landmarkβs country
π Analyse the data
Each record contains:
- The name of a famous landmark [item 0]
- The country in which it is found [item 1]
sites = [
["Mount Everest", "Nepal"],
["Grand Canyon", "United States"],
["Kilamanjaro", "Tanzania"],
["Mount Fuji", "Japan"],
["Uluru", "Australia"],
["Mount Etna", "Italy"],
["Wistmans Wood", "UK"]
]
π What should happen
- The user will be prompted to input the name of a landmark
- The program will then search all records in the list for a match
- If a match is found, the program will output the name of the country
Enter a landmark: Uluru Uluru is found in Australia
π (1) Create a user input - for name of landmark
- The user will be prompted to input the name of a landmark
- This will be assigned to a variable called lm
Click to see how the code should look
lm = input("Enter the name of a famous landmark: ")
π (2) Write the conditions for a for loop
In order to search for the country of the landmark entered:
- The code must use a for loop
- The for loop will iterate as many times as there are records in the list
Click to see how the code should look
for i in range(0, 7):
π (3) Use selection (IF / ELSE) to find a match
Use an IF / ELSE statement to identify if the user input is a match for the landmark in each record
Click to see how the code should look
for i in range(0, 7):
if lm == sites[i][0]:
π (4) Output the name of the country for a match
Output the landmark searched for and the name of its country
Uluru is found in australia
Click to see how the code should look
for i in range(0, 7):
if lm == sites[i][0]:
print(f"{lm} is found in {sites[i][1]}")
π Activity 3 - Top scoring house points
π Understand the data
Again you are given a 2D list called students.
Each record contains:
- First name [item 0]
- House name [item 1]
- House points [item 2]
students = [
["Cassie", "Robertson", 33],
["Emily", "Wilkinson", 31],
["Lydia", "Beale", 43],
["Nerys", "Jackson", 22],
["Parul", "Cross", 38],
["Amelia", "Robertson", 28],
["Sophie", "Wilkinson", 34],
["Hannah", "Beale", 39],
["Freya", "Jackson", 26],
["Aisha", "Cross", 35]
]
π What should happen
- The user will be prompted to input a minimum house point score
- The program will then search the list for records with this score or above
- The program will then output the name of each matching student and their score
Enter a minimum number of house points: 38 Students with 38 or more house points: Lydia - 43 Parul - 38 Hannah - 39
π (1) Create a user input
- The user will be prompted to input a minimum number of house points
- As the input will be a number, it must be cast to integer
- This will be assigned to a variable called hp
Click to see how the code should look
hp = int(input("Enter a minimum number of house points: "))
π (2) Confirm the selection
- The program will output a message
- It will confirm the minimum number of house points selected
Students with 38 or more house points:
Click to see how the code should look
print(f"Students with {hp} or more house points:")
π (3) Create conditions for a for loop
- A for loop is created
- It will use a range from 0 to the length of the students list
- See below for details
Click to see how the code should look
By using the len() function we don't need to know how long the list is
Python will work this out for us
for i in range(0, len(students)):
π (4) Records with house points greater than or equal to the input number
Use selection (IF / ELSE) to check if the number of house points is greater than or equal to the input
Click to see how the code should look
for i in range(0, len(students)):
if students[i][2] >= hp:
π (5) Output the names of students who match the criteria
Use an f string to output the names of any students who match the criteria
Click to see how the code should look
for i in range(0, len(students)):
if students[i][2] >= hp:
print(f"{students[i][0]} - {students[i][2]} house points.")
π Activity 4 - Your Own Search
π Understand the task
- Create a 2D list based on a topic of your choice
- Enable the user to search through the records of your 2D list